//势力占领地图，势力自身的沙盘地图(在敌占区，敌人可能已经构筑了防御工事或破坏了路桥，但本势力不知道)
//势力占领地图用法：构造
//势力自身的沙盘地图用法：构造，刷新某个地块情报，刷新某个地块及其邻接地块情报，
//势力自身的沙盘地图用法：判断某个单位是否可见，根据沙盘获取路径代价地图，根据沙盘获取某个单位的路径代价地图


//标识每个势力占领区域（每个tick搜集一次）
//占领标识为哪个势力的部队到达该区域更快，不计算部队移动速度参数
//return: { zone:[0, 0, 1, 2, 2, 3] }  其中的数字为控制该tile的势力ID
var ForceCapture = function(campaign, tileEngine){
	this.forceMap = CreateArray(0, tileEngine.tileCount, -1, false);
	var minpayMap = CreateArray(0, tileEngine.tileCount, 5000, false);
	//部队到达
	for (var troopid = 0; troopid < campaign.troop.length; troopid++){
		if (campaign.troop[troopid].strength <= 0) continue;
		
		if (campaign.troop[troopid].court_force)
			continue;

		var forceid = campaign.troop[troopid].force;
		
		var troopPayMap = tileEngine.MapPay(campaign.zone.pass_index, campaign.troop[troopid].tileIndex, 50000);
		for (var idx = 0; idx < minpayMap.length; idx++){
			if (troopPayMap[idx].pay < minpayMap[idx]){
				minpayMap[idx] = troopPayMap[idx].pay;
				this.forceMap[idx] = forceid;
			}
		}
	}
	//势力点到达
	for (var forceid = 0; forceid < campaign.force.length; forceid++){
		var forcepointPayMap = tileEngine.MapPay(campaign.zone.pass_index, campaign.force[forceid].tileIndex, 50000);
		for (var idx = 0; idx < minpayMap.length; idx++){
			if (forcepointPayMap[idx].pay < minpayMap[idx]){
				minpayMap[idx] = forcepointPayMap[idx].pay;
				this.forceMap[idx] = forceid;
			}
		}
	}
};

//标识每个势力已收集的区域情报
//1.每个tick扫描战争迷雾，并获取其情报
//2.只要部队经过就扫描，每个tick可能搜集多次）
var ForceInfo = {
	//1.获得forceCapture及其邻域为可视区域
	//2.获得所有军队是否可见的信息
	//3.function 通过forceCapture刷新ForceInfo
	//4.function 通过troop移动后所在位置，动态刷新ForceInfo
	//5.function 获得当前的通过地图，敌军部队所在地区视为不可通过（障碍指数5000）

	Create : function(forceid, zone, tileEngine, forceCapture){
		var forceInfo = {
			forceid : forceid,
			seeMap : [], //各tile的可视性 [true, true, false, false,.....]
			pass_index : DeepClone(zone.pass_index),
			defend_index : DeepClone(zone.defend_index)
		};
		this.RefreshByCapture(forceInfo, zone, tileEngine, forceCapture);
		return forceInfo;
	},

	RefreshTile : function(forceInfo, tileIndex, zone){
		forceInfo.seeMap[tileIndex] = true;
		forceInfo.pass_index[tileIndex] = zone.pass_index[tileIndex];
		forceInfo.defend_index[tileIndex] = zone.defend_index[tileIndex];
	},
	
	RefreshByCapture : function(forceInfo, zone, tileEngine, forceCapture){
		for (var tileIndex = 0; tileIndex < forceCapture.forceMap.length; tileIndex++)
			forceInfo.seeMap[tileIndex] = false;
	
		for (var tileIndex = 0; tileIndex < forceCapture.forceMap.length; tileIndex++){
			if (forceCapture.forceMap[tileIndex] == forceInfo.forceid){
				this.RefreshTileAndNeighbor(forceInfo, tileIndex, zone, tileEngine);
			}
		}
	},
	
	RefreshTileAndNeighbor : function(forceInfo, tileIndex, zone, tileEngine){
		if (forceInfo.seeMap[tileIndex] == false){
			this.RefreshTile(forceInfo, tileIndex, zone);
		}
		for (var neighborid in tileEngine.neighborCache[tileIndex]){
			var tgtindex = tileEngine.neighborCache[tileIndex][neighborid];
			if (forceInfo.seeMap[tgtindex] == false){
				this.RefreshTile(forceInfo, tgtindex, zone);
			}
		}
	},
	
	//计算势力地图，由本方情报提供代价地图，可以看见的敌军位置会被标识为不可到达区域
	GetForcePassIndex : function(forceInfo, troop, tileEngine, isAvoidEnemy, isAvoidCrowed){
		var pass_index = DeepClone(forceInfo.pass_index);
		if (isAvoidEnemy){
			var troopVisibility = this.GetTroopVisibility(forceInfo, troop, tileEngine);
			for (var troopid = 0; troopid < troopVisibility.length; troopid++){
				if (troop[troopid].strength <= 0) continue;
				
				if (troop[troopid].court_force)
					continue;
				
				if (troop[troopid].force != forceInfo.forceid && troopVisibility[troopid])
					pass_index[ troop[troopid].tileIndex ] = -1;
			}
		}
		if (isAvoidCrowed){
			var crowedMap = troopCrowed.GetScriptCrowedMap(troop, tileEngine);
			for (var tileIndex = 0; tileIndex < tileEngine.tileCount; tileIndex++){
				if (crowedMap[tileIndex] > 3)
					pass_index[tileIndex] = -1;
			}
		}
		return pass_index;
	},
	
	GetTroopPassIndex : function(forceInfo, campaign, tileEngine, troopid, isAvoidEnemy, isAvoidCrowed){
		var pass_index = this.GetForcePassIndex(forceInfo, campaign.troop, tileEngine, isAvoidEnemy, isAvoidCrowed);
		for (var tileIndex = 0; tileIndex < pass_index.length; tileIndex++){
			if (campaign.troop[troopid].move_ability[campaign.zone.land_type[tileIndex]] == false){
				pass_index[tileIndex] = -1;
			}
		}
		return pass_index;
	},
	
	GetTroopVisibility : function(forceInfo, troop, tileEngine){
		var troopVisible = [];
		for (var troopid in troop){
			if (troop[troopid].strength <= 0) continue;

			troopVisible[troopid] = forceInfo.seeMap[troop[troopid].tileIndex];
		}
		return troopVisible;
	}
};